<?php

/**
 *  @author  Lynn Cho <lynncho@live.com>
 *  @createTime  2016-8-31 17:49:45
 *  @copyright  shewen technology
 *  @descrition  
 */
class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('LoginModel');
        $this->load->helper('url');
        $this->load->library('session');
    }

    /**
     * home page of visitor, login
     */
    public function index() {
        if ($this->session->has_userdata('manager')) {
            echo "<script>alert('您已登录！');location='" . $this->config->item('base_url') . "system';</script>";
            exit;
        }

        if ($_POST) {
            $username = @htmlspecialchars(trim($this->input->post('username')));
            $password = @htmlspecialchars(trim($this->input->post('password')));
            $res = $this->LoginModel->verifyLogin($username, $password);
            if (empty($res)) {
                echo "<script>alert('未知错误！');location='" . $this->config->item('base_url') . "login';</script>";
            } else if ($res === 401) {
                echo "<script>alert('用户名或密码不正确！');location='" . $this->config->item('base_url') . "login';</script>";
            } elseif ($res === 403) {
                echo "<script>alert('用户名或密码不允许为空！');location='" . $this->config->item('base_url') . "login';</script>";
            } else {
                $this->session->set_userdata('manager', $res);
                redirect('/system');
            }
            exit;
        }

        $this->load->view('login');
    }

    public function logout() {
        $this->session->manager = null;
        echo "<script>alert('注销成功！');location='" . $this->config->item('base_url') . "login';</script>";
    }

}
